import express from "express"
import cors from 'cors'
import path from 'path'
import http from 'http'
import { fileURLToPath } from 'url'
import router from './src/router/router.js'
import CheckTokenWare from './src/middleware/tokenWare.js'
import errorWare from "./src/middleware/errorWare.js"
import { ParseUtil } from "./src/util/parse.js"
import _InitGlobal from "./src/util/definedProperty.js"
import _initWebSocket from "./src/util/webSocket.js"
import handleQuery from "./src/util/handleQuery.js"

// const __dirname_huily = path.dirname(fileURLToPath(import.meta.url));  // 当前文件的绝对路径
const __filename_huily = new URL(import.meta.url).pathname;  // 路径，包含文件
const __dirname_huily = path.dirname(__filename_huily);   // 路径，目录

_InitGlobal({ __dirname_huily })

const app = express();

const server = http.createServer(app)

_initWebSocket(server)

app.use(express.urlencoded({ extended: false })) // 处理form格式的post参数

app.use(express.json()) // 处理json格式的post参数

app.use(handleQuery) // 处理query参数Boolean值转字符串问题

app.use(cors({ exposedHeaders: "*", allowedHeaders: "*" }))   // 跨域请求以及自定义头部暴露

app.use(ParseUtil.parseIP) // 处理ip地址

app.use(CheckTokenWare); // 验证Token 正则排除首页、注册和登录

app.use(router); // 分发router

app.use('/static', express.static('static'))

app.use(errorWare); // 错误处理

server.listen('8088', () => { console.log('端口8088在监听!') })
